package logic

import (
	"context"

	"github.com/comfygpt-backend-go/common/consts"
	"github.com/comfygpt-backend-go/common/utils"
	"github.com/comfygpt-backend-go/service/component-api/internal/svc"
	"github.com/comfygpt-backend-go/service/component-api/internal/types"
	"github.com/comfygpt-backend-go/service/component-rpc/component"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateComponentLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateComponentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateComponentLogic {
	return &UpdateComponentLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateComponentLogic) UpdateComponent(req *types.ComponentItem) (resp *types.CommonResp, err error) {
	// 初始化响应
	resp = &types.CommonResp{
		Code:    int64(consts.SUCCESS),
		Message: consts.WrongMessageEn[consts.SUCCESS],
	}

	// 校验参数
	item := &component.ComponentItem{
		Id:          req.Id,
		Name:        req.Name,
		Description: req.Description,
		Category:    req.Category,
		Icon:        req.Icon,
		Tags:        utils.StringToArray(req.Tags),
	}

	// 创建组件
	_, err = l.svcCtx.ComponentRpc.UpdateComponent(l.ctx, item)
	if err != nil {
		resp.Code = int64(consts.SERVER_WRONG)
		resp.Message = consts.WrongMessageEn[consts.SERVER_WRONG]
		return
	}

	return
}
